﻿namespace Leetcode.N0953;

public class Solution
{
    public bool IsAlienSorted(string[] words, string order)
    {
        var letterOrder = new int[26];
        for (int i = 0; i < order.Length; i++)
        {
            letterOrder[order[i] - 'a'] = i;
        }
        for (int i = 1; i < words.Length; i++)
        {
            if (!this.Check(words[i - 1], words[i], letterOrder))
            {
                return false;
            }
        }
        return true;
    }
    private bool Check(string word1, string word2, int[] order)
    {
        var length = Math.Min(word1.Length, word2.Length);
        for (int i = 0; i < length; i++)
        {
            var order1 = order[word1[i] - 'a'];
            var order2 = order[word2[i] - 'a'];
            if (order1 > order2)
            {
                return false;
            }
            if (order1 < order2)
            {
                return true;
            }
        }
        return word1.Length <= word2.Length;
    }
}
